<?php
if(!isset($_SESSION['subtotal'])) $_SESSION['subtotal'] = 0;
if($_REQUEST['ajax'] == 'true'){
	session_start();
	include "../../connections/con_gl.php";
	
	switch($_REQUEST['act']){
		case 'add' :
			$_SESSION['kendaraan'][] 	= $_POST['kendaraan'];
			$_SESSION['txtkendaraan'][] = $_POST['txtkendaraan'];
			$_SESSION['hargasatuan'][] 	= $_POST['hargasatuan'];
			$_SESSION['diskon'][]		= $_POST['diskon'];
			$_SESSION['total'][]		= $_POST['total'];
			$_SESSION['transaksi'][]	= $_POST['transaksi'];
			$_SESSION['subtotal']		+= $_POST['total'];
			$_SESSION['pajak']			= $_SESSION['subtotal']*($_POST['pajak']/100);
			$_SESSION['grandtotal']		= $_SESSION['subtotal']+$_POST['biayalain']+$_SESSION['pajak'];
			break;
			
		case 'delete' :
			$pos = $_POST['pos'];
			
			$_SESSION['subtotal'] -= $_SESSION['total'][$pos];
			$_SESSION['pajak']			= $_SESSION['subtotal']*($_POST['pajak']/100);
			$_SESSION['grandtotal']		= $_SESSION['subtotal']+$_POST['biayalain']+$_SESSION['pajak'];
			
			unset($_SESSION['kendaraan'][$pos]);
			unset($_SESSION['txtkendaraan'][$pos]);
			unset($_SESSION['hargasatuan'][$pos]);
			unset($_SESSION['diskon'][$pos]);
			unset($_SESSION['total'][$pos]);
			unset($_SESSION['transaksi'][$pos]);
			
			$_SESSION['kendaraan']		=  array_values($_SESSION['kendaraan']);
			$_SESSION['txtkendaraan']	=  array_values($_SESSION['txtkendaraan']);
			$_SESSION['hargasatuan']	=  array_values($_SESSION['hargasatuan']);
			$_SESSION['diskon']			=  array_values($_SESSION['diskon']);
			$_SESSION['total']			=  array_values($_SESSION['total']);
			$_SESSION['transaksi']		=  array_values($_SESSION['transaksi']);
			break;
	}
}
?>
<table width="100%" border="0" cellspacing="0" cellpadding="5" class="datatable">
	<tr>
		<th width="32%" align="center"><strong>Pilih Kendaraan</strong></th>
		<th width="15%" align="center"><strong>Harga </strong></th>
		<th width="5%" align="center"><strong>Diskon</strong></th>
		<th width="15%" align="center"><strong>Total</strong></th>
		<th align="center"><strong>Info</strong></th>
		<th width="6%" align="center"><strong>Aksi</strong></th>
	</tr>
	<?php
	if(isset($_SESSION['kendaraan'])){
		$i=0;
		foreach($_SESSION['kendaraan'] as $idk){
			echo "<tr>";
				echo "<td>".($_SESSION['txtkendaraan'][$i])."</td>";
				echo "<td align='right'>Rp. ".number_format($_SESSION['hargasatuan'][$i],0,',','.')."</td>";
				echo "<td align='center'>".($_SESSION['diskon'][$i])."%</td>";				
				echo "<td align='right'>Rp. ".number_format($_SESSION['total'][$i],0,',','.')."</td>";
				echo "<td>".($_SESSION['transaksi'][$i])."</td>";
				echo "<td align='center'><a style='cursor:pointer;' onclick=\"javascript: kirimRequest('components/kontrak/detail.php', 'ajax=true&act=delete&pos=$i&biayalain='+document.getElementById('biayalain').value+'&pajak='+document.getElementById('pajak').value, 'ajaxDiv', 'div');\"><img src='images/delete.png' /></a></td>";
			echo "</tr>";
			$i++;
		}
	}
	?>	
    <tr valign="top" bgcolor="#FFFFFF">
        <td align="left" style="background:none;border:none;"> 
			<!-- Added by suwondo -->
			<input type="hidden" name="kendaraan" id="kendaraan" />
			<input type="text" name="txtkendaraan" id="txtkendaraan" placeholder="Cari Kendaraan ..." size="60" readonly style="background-color:#fff;border:1px solid #999; height:16px;" onclick="NewWindow('index.php?component=kendaraan&amp;task=list&amp;barang=false&amp;open=window','name','825','450','yes');return false" />
		</td>
        <td align="right" style="background:none;border:none;"><label id="divHargaKendaraan">              
            Rp. <input name="hargasatuan" type="text" id="hargasatuan" onkeyup="javascript:if(this.value=='' || isNaN(this.value)) this.value='0'; this.value=parseInt(this.value); this.form.total.value=this.form.hargasatuan.value - (this.form.hargasatuan.value * this.form.diskon.value / 100);" size="10" maxlength="10" autocomplete="off" />              
            </label>
		</td>
        <td align="center" style="background:none;border:none;"><label>
			<input name="diskon" type="text" id="diskon" onkeyup="javascript:if(this.value=='' || isNaN(this.value)) this.value='0'; this.value=parseInt(this.value); this.form.total.value=this.form.hargasatuan.value - (this.form.hargasatuan.value * this.form.diskon.value / 100);" value="0" size="2" maxlength="3" autocomplete="off" />
              </label>%
        </td>
		<td align="right" style="background:none;border:none;">
			<label>Rp. <input name="total" type="text" id="total" size="10" maxlength="15" /></label>
		</td>
		<td align="center" style="background:none;border:none;">
			<label><textarea name="transaksi" cols="40" rows="2" id="transaksi"></textarea></label>
		</td>
		<td align="center" style="background:none;border:none;">
			<input name="add" type="button" id="add" value="&nbsp;" onclick="var err=''; 
			if(document.getElementById('kendaraan').value=='') err+='kendaraan harus dipilih\n'; 
			if(document.getElementById('txtkendaraan').value=='') err+='kendaraan harus dipilih\n'; 
			if(document.getElementById('hargasatuan').value=='') err+='Harga harus diisi\n'; 
			if(err!='') alert(err);
			else kirimRequest('components/kontrak/detail.php', 'ajax=true&act=add&kendaraan='+document.getElementById('kendaraan').value+'&txtkendaraan='+document.getElementById('txtkendaraan').value+'&hargasatuan='+document.getElementById('hargasatuan').value+'&diskon='+document.getElementById('diskon').value+'&total='+document.getElementById('total').value+'&transaksi='+document.getElementById('transaksi').value+'&biayalain='+document.getElementById('biayalain').value+'&pajak='+document.getElementById('pajak').value, 'ajaxDiv', 'div');" class="checkbutton" title="Tambah Transaksi"/>
		</td>
	</tr>
	<tr>
		<td width="55%" align="right" colspan="3"><strong>Subtotal</strong></td>
		<td width="12%" align="right"><strong>Rp. <label id="subtotal"><?php if($_SESSION['subtotal']=='') echo "0"; else echo number_format($_SESSION['subtotal'],0,',','.'); ?></label></strong></td>
		<td align="center"><strong>&nbsp;</strong></td>
		<td width="6%" align="center"><strong>&nbsp;</strong></td>
	</tr>
	<tr>
		<td width="55%" align="right" colspan="3"><strong>Biaya Lain</strong></td>
		<td width="12%" align="right">Rp. <input type="text" name="biayalain" id="biayalain" size="10" value="<?php if(isset($_POST['biayalain'])) echo $_POST['biayalain']; else echo "0"; ?>" style="text-align:right;" onkeyup="javascript: if(this.value=='' || isNaN(this.value)) this.value='0'; this.value=parseInt(this.value); var pajak=<?php echo $_SESSION['subtotal']; ?>*(document.getElementById('pajak').value/100); grandtotal=<?php echo $_SESSION['subtotal']; ?>+parseInt(document.getElementById('biayalain').value)+(<?php echo $_SESSION['subtotal']; ?>*document.getElementById('pajak').value/100); document.getElementById('grandtotal').innerHTML=number_format(grandtotal,0,',','.');" /></td>
		<td align="center"><strong>&nbsp;</strong></td>
		<td width="6%" align="center"><strong>&nbsp;</strong></td>
	</tr>
	<tr>
		<td width="55%" align="right" colspan="3"><strong>Pajak</strong>&nbsp;&nbsp;<input type="text" name="pajak" id="pajak" size="3" value="<?php if(isset($_POST['pajak'])) echo $_POST['pajak']; else echo "0"; ?>" style="text-align:right;" onkeyup="javascript: if(this.value=='' || isNaN(this.value)) this.value='0'; this.value=parseInt(this.value); pajakval=<?php echo $_SESSION['subtotal']; ?>*parseInt(this.value)/100; document.getElementById('pajakval').innerHTML=number_format(pajakval,0,',','.'); grandtotal=<?php echo $_SESSION['subtotal']; ?>+parseInt(document.getElementById('biayalain').value)+(<?php echo $_SESSION['subtotal']; ?>*parseInt(this.value)/100); document.getElementById('grandtotal').innerHTML=number_format(grandtotal,0,',','.');" autocomplete="off" />&nbsp;%</td>
		<td width="12%" align="right"><strong>Rp. <label id="pajakval"><?php echo number_format($_SESSION['pajak'],0,',','.'); ?></label></strong></td>
		<td align="center"><strong>&nbsp;</strong></td>
		<td width="6%" align="center"><strong>&nbsp;</strong></td>
	</tr>
	<tr>
		<td width="55%" align="right" colspan="3"><strong>Grand Total</strong></td>
		<td width="12%" align="right"><strong>Rp. <label id="grandtotal"><?php echo number_format($_SESSION['grandtotal'],0,',','.'); ?></label></strong></td>
		<td align="center"><strong>&nbsp;</strong></td>
		<td width="6%" align="center"><strong>&nbsp;</strong></td>
	</tr>
</table>